package com.example.user.myfirstapp.common;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.Base64;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.example.user.myfirstapp.R;

import java.io.ByteArrayOutputStream;

/**
 * Created by user on 2016/6/12.
 */
public class ImageHelper {
    /**
     * 获取NetWorkImageView内容
     * @param requestQueue
     * @param imageView
     * @param imageUrl
     */
    public static void showImageByNetworkImageView(RequestQueue requestQueue, NetworkImageView imageView, String imageUrl) {
        if (imageUrl != null && !imageUrl.equals("")) {
            ImageLoader imageLoader = new ImageLoader(requestQueue, BitmapCache.instance());
            if(imageView!=null)
            {
                imageView.setTag("url");
                //imageView.setErrorImageResId(R.drawable.empty_photo);
                imageView.setImageUrl(imageUrl, imageLoader);
            }
        }
    }

    /**
     * 通过Base将Bitmap转换成Base64字符串
     * @param bit
     * @return
     */
    public static String BitmapToStrByBase64(Bitmap bit){
        if(bit==null)
            return "";
        ByteArrayOutputStream bos=new ByteArrayOutputStream();
        bit.compress(Bitmap.CompressFormat.JPEG, 40, bos);//参数100表示不压缩
        byte[] bytes=bos.toByteArray();
        return Base64.encodeToString(bytes, Base64.DEFAULT);
    }
}
